package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户表(User)表控制层
 *
 * @author makejava
 * @since 2024-01-13 17:16:26
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;

    /**
     * 加载所有用户
     *
     * @return
     */
    @GetMapping("/loadAllUser")
    public Result loadAllUser() {
        return Result.success(userService.loadAllUser());
    }

    /**
     * 获取所有用户信息
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        return Result.success(userService.selectAll());

    }

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        userService.deleteById(id);
        return Result.success("删除成功!");
    }

    /**
     * 更新用户信息
     *
     * @param user
     * @return
     */
    @PutMapping("/updateById")
    public Result updateById(@RequestBody User user) {
        userService.updateById(user);
        return Result.success("更新成功！");
    }


    @PostMapping("/updateSimpleById")
    public Result updateSimpleById(@RequestBody User user) {
        userService.updateSimpleById(user);
        return Result.success("更新成功！");
    }

    @PostMapping("/changePasswordById")
    public Result changePasswordById(@RequestParam("id") Integer id,
                                     @RequestParam("password") String password,
                                     @RequestParam("newPassword") String newPassword) {
        userService.changePasswordById(id, password, newPassword);
        return Result.success("更新成功！");
    }


//    /**
//     * 条件查询
//     * @param username
//     * @param name
//     * @param phone
//     * @param email
//     * @return
//     */
//    @GetMapping("/selectByManyCondition")
//    public Result selectByManyCondition(@RequestParam("username") String username,
//                                        @RequestParam("name") String name,
//                                        @RequestParam("phone") String phone,
//                                        @RequestParam("email") String email){
//        try {
//            return Result.success(userService.selectByManyCondition(username,name,phone,email));
//        }catch (Exception e){
//            return  Result.error("查询失败！  ----->    " + e.getMessage());
//        }
//    }


    /**
     * 条件查询
     *
     * @param username
     * @param name
     * @param phone
     * @param status
     * @return
     */
    @GetMapping("/selectByManyCondition")
    public Result selectByManyCondition(@RequestParam("username") String username,
                                        @RequestParam("name") String name,
                                        @RequestParam("phone") String phone,
                                        @RequestParam("status") Integer status) {
        return Result.success(userService.selectByManyCondition(username, name, phone, status));
    }

//    /**
//     * 条件查询   分页
//     * @param currentPage
//     * @param pageSize
//     * @param username
//     * @param name
//     * @param phone
//     * @param email
//     * @return
//     */
//    @GetMapping("/selectByManyConditionInpage")
//    public Result selectByManyConditionInpage(@RequestParam("currentPage") Integer currentPage,
//                                        @RequestParam("pageSize") Integer pageSize,
//                                        @RequestParam("username") String username,
//                                        @RequestParam("name") String name,
//                                        @RequestParam("phone") String phone,
//                                        @RequestParam("email") String email){
//        try {
//            return Result.success(userService.selectByManyConditionInpage(currentPage,pageSize,username,name,phone,email));
//        }catch (Exception e){
//            return  Result.error("查询失败！  ----->    " + e.getMessage());
//        }
//    }


    /**
     * 条件查询   分页
     *
     * @param currentPage
     * @param pageSize
     * @param username
     * @param name
     * @param phone
     * @param status
     * @return
     */
    @GetMapping("/selectByManyConditionInpage")
    public Result selectByManyConditionInpage(@RequestParam("currentPage") Integer currentPage,
                                              @RequestParam("pageSize") Integer pageSize,
                                              @RequestParam("username") String username,
                                              @RequestParam("name") String name,
                                              @RequestParam("phone") String phone,
                                              @RequestParam("status") Integer status) {
        return Result.success(userService.selectByManyConditionInpage(currentPage, pageSize, username, name, phone, status));
    }

    /**
     * 条件查询   分页
     *
     * @param currentPage
     * @param pageSize
     * @param username
     * @param name
     * @param phone
     * @return
     */
    @GetMapping("/selectByManyConditionNoStatusInpage")
    public Result selectByManyConditionNoStatusInpage(@RequestParam("currentPage") Integer currentPage,
                                                      @RequestParam("pageSize") Integer pageSize,
                                                      @RequestParam("username") String username,
                                                      @RequestParam("name") String name,
                                                      @RequestParam("phone") String phone) {
        return Result.success(userService.selectByManyConditionNoStatusInpage(currentPage, pageSize, username, name, phone));
    }


    /**
     * 分页查询
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam("currentPage") Integer currentPage,
                               @RequestParam("pageSize") Integer pageSize) {
        return Result.success(userService.selectByPage(currentPage, pageSize));
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        userService.deleteByIds(ids);
        return Result.success("删除成功！");
    }


    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user) {
        userService.addUser(user);
        return Result.success("添加成功！");
    }


    /**
     * 用户详情
     *
     * @param id
     * @return
     */
    @GetMapping("/userInfo")
    public Result userInfo(@RequestParam("id") Integer id) {
        return Result.success(userService.selectById(id));
    }


    @PostMapping("/getTotal")
    public Result getTotal() {
        return Result.success(userService.getTotal());
    }


    @PostMapping("/updateAddressById")
    public Result updateAddressById(@RequestParam("id") Integer id,
                                    @RequestParam("address") String address) {
        userService.updateAddressById(id, address);
        return Result.success("修改成功！");
    }


    @PostMapping("/updateNameAndAddressAndPhoneById")
    public Result updateNameAndAddressAndPhoneById(@RequestParam("id") Integer id,
                                                   @RequestParam("name") String name,
                                                   @RequestParam("address") String address,
                                                   @RequestParam("phone") String phone) {
        userService.updateNameAndAddressAndPhoneById(id, name, address, phone);
        return Result.success("修改成功！");
    }


    @PostMapping("/getUserById")
    public Result getUserById(@RequestParam("id") Integer id) {
        return Result.success(userService.getUserById(id));
    }

}




